
package com.dp.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpSession;
//import javax.servlet.http.HttpSession;
public class BaseBean {
    public static boolean StringIsNull(Object str) {
        boolean b = false;
        try {
            if (!str.equals("") && !str.equals("null")) {
                b = true;
            }
        } catch (Exception e) {
        }
        return b;
    }
    public static boolean StringIsNotNull(Object str) {
        boolean b = false;
        try {
            if (!str.equals("") && !str.equals("null")) {
                b = true;
            }
        } catch (Exception e) {
        }
        return b;
    }
    public static String substring(String string, int i, int j) {
        try {
            return string.substring(i, j);
        } catch (Exception e) {
            return "";
        }
    }
    public static String substring(String string, String mark, int i) {
        try {
            return string.substring(string.indexOf(mark), i);
        } catch (Exception e) {
            return "";
        }
    }
    public static String substring(String string, String mark1, String mark2) {
        try {
            return string.substring(string.indexOf(mark1), string.indexOf(mark2));
        } catch (Exception e) {
            return "";
        }
    }
    public static String substring(String string, int i, String mark) {
        try {
            return string.substring(i, string.indexOf(mark));
        } catch (Exception e) {
            return "";
        }
    }
    public static String ObjectToStringDB(Object obj) {
        try {
            String str = obj.toString().trim();

            if (str.equals("") || str.equals("null")) {
                return "null";
            }

            return "'" + str + "'";
        } catch (Exception e) {
            return "null";
        }
    }
    public static String ObjectToString(Object obj) {
        try {
            return obj.toString().trim();
        } catch (Exception e) {
            return "";
        }
    }
    public static int getint(Object value) {
        try {
            return Integer.parseInt(ObjectToString(value));
        } catch (Exception e) {
            return 0;
        }
    }
    public static int getint(Object value, int radix) {
        try {
            return Integer.parseInt(ObjectToString(value), radix);
        } catch (Exception e) {
            return 0;
        }
    }
    public static long getlong(Object value) {
        try {
            return Long.parseLong(ObjectToString(value));
        } catch (Exception e) {
            return 0;
        }
    }
    public static float getfloat(Object value) {
        try {
            return Float.parseFloat(ObjectToString(value));
        } catch (Exception e) {
            return 0f;
        }
    }
    public static double getdouble(Object value) {
        try {
            return Double.parseDouble(ObjectToString(value));
        } catch (Exception e) {
            return 0.0;
        }
    }
    public static String getName(String s) {
        s = s.substring(0, 1).toUpperCase() +
            s.substring(1, s.length()).toLowerCase();

        return s;
    }
    public static String ChangeString(String value, String charset) {
        try {
            value = ObjectToString(value);
            value = new String(value.getBytes(charset));
        } catch (Exception e) {
            e.getMessage();
        }

        return value;
    }
    public static String getNowTime(String timeFormat) {
        SimpleDateFormat lformat = new SimpleDateFormat(timeFormat);
        Calendar now = Calendar.getInstance();
        String nowstr = lformat.format(now.getTime());

        return nowstr;
    }
    public static String getNowTime() {
        SimpleDateFormat lformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar now = Calendar.getInstance();        
		now.setTime(now.getTime());
        String nowstr = lformat.format(now.getTime());

        return nowstr;
    }
    public static String getStrDate(Date date) {
        SimpleDateFormat lformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datestr = lformat.format(date);
        return datestr;
    }
    public static void  main(String a[])
    {
    
    	
  	
		Calendar   cal     =   Calendar.getInstance();   
  		Date date=new Date();
  		date.setHours(0);
  		date.setMinutes(0);
  		date.setSeconds(0);
  		cal.setTime(date);   
  		cal.add(GregorianCalendar.MONTH,1); 
  		
  		int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(cal.getTime()));
		int month=Integer.parseInt(new SimpleDateFormat("MM").format(cal.getTime()));
		int day=Integer.parseInt(new SimpleDateFormat("dd").format(cal.getTime()));
		
		
		//cal.set(year, month, date, 0, 0, 0);
  		
  		SimpleDateFormat   theDate   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  		String   dateString   =   theDate.format(cal.getTime());   
  		
    }
    public static DataMap getAttributeNames(HttpSession session) {
        DataMap dataMap = new DataMap();
        Enumeration enumeration = session.getAttributeNames();
        String object = null;
        while (enumeration.hasMoreElements()) {
            object = BaseBean.ObjectToString(enumeration.nextElement());
            dataMap.put(object, session.getAttribute(object));
        }
        return dataMap;
    }
    public static DataMap getAttributeNames(DataMap dataMap,HttpSession session) {       
        Enumeration enumeration = session.getAttributeNames();
        String object = null;
        while (enumeration.hasMoreElements()) {
            object = BaseBean.ObjectToString(enumeration.nextElement());
            dataMap.put(object, session.getAttribute(object));
        }
        return dataMap;
    }
	public static String encode(String string) {
        StringBuffer result = new StringBuffer();
        int i;

        for (i = 0; i < string.length(); i++) 
        {
           if(" ".equals(""+string.charAt(i)))
           {
           		result.append("%20");
           		continue;
           }
           
           else if("������������*������������������������������".indexOf(""+string.charAt(i))!=-1)//���ı�� ��֪��ȡֵ��Χ ��ʱ����
           {
           	
           		result.append(java.net.URLEncoder.encode(""+string.charAt(i)).toLowerCase());
           		continue;
           }
            if (string.charAt(i) >= 0x4e00) {
               
                String hex = ""+string.charAt(i);                
                hex=java.net.URLEncoder.encode(hex);
                hex=hex.toLowerCase();
                result.append(hex);
            } else 
            {
            	result.append(string.charAt(i));                
                
            }
        }
		
        return result.toString();
    }
}
